package com.qingyun.form.entity

class FormSelectItem(
    var label: String,
    var value: String,
    val name: String? = null,
    val order: Int = -1,
) {
    internal var parent: FormSelectItem? = null
    internal var children: MutableList<FormSelectItem>? = null
    internal val hasChildren: Boolean
        get() = !children.isNullOrEmpty()

    fun addChild(child: FormSelectItem): FormSelectItem {
        if (children == null) {
            children = mutableListOf()
        }
        child.parent = this
        children!!.add(child)
        children!!.sortWith { a, b ->
            when {
                a.hasChildren -> -1
                b.hasChildren -> 1
                else -> 0
            }
        }
        return this
    }
}